<template>
  <div class="login-container">
    <video src="../../static/video.3cf57b17.mp4" autoplay="autoplay" loop="loop" muted="muted" style="width: 100%; height: 100%; object-fit: fill;" />
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <img src="../../assets/logo1.33ef7fa0.png" alt="">
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.account"
          placeholder="账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="key">
        <span class="svg-container">
          <svg-icon icon-class="yanzhengma" />
        </span>
        <el-input v-model="loginForm.code" placeholder="验证码" />
        <img data-v-548e52e0="" :src="blob" alt="codeImage" class="code-image" @click="hCaptcha">
      </el-form-item>

      <el-button type="primary" class="login-button" style="width:100%;margin-bottom:30px;" @click.native.native="handleLogin"> <span v-loading="loading" class="load" element-loading-spinner="el-icon-loading" target="el-button" /><span>登录</span></el-button>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { randomNum } from '@/utils'
import { getCaptcha } from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (validUsername(value)) {
        callback(new Error('账号不能为空'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    const validateKey = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('验证码不能为空'))
      } else {
        callback()
      }
    }
    return {
      blob: null,
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: '',
        tenant: '0000',
        key: '',
        bindAccount: '',
        bindPassword: '',
        signAccount: '',
        signPassword: ''
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', validator: validateKey }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.hCaptcha()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async hCaptcha() {
      const re = randomNum()
      this.loginForm.key = re
      const res = await getCaptcha(re)
      const blob = new Blob([res.data])
      const url = window.URL.createObjectURL(blob)
      this.blob = url
    },
    async hLogin() {
      await this.$store.dispatch('user/doToken', this.loginForm)
      this.$router.push('/')
    },
    handleLogin() {
      this.loading = true
      this.$refs.loginForm.validate(async valid => {
        if (valid) this.hLogin()
        this.loading = false
      })

      if (this.$router.history.current.path === '/login') {
        setTimeout(() => {
          this.loading = false
        }, 3000)
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  background-color: transparent !important;
  .login-button.loading{
        border-color: #EB907C !important;
      }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 100%;
    color: #2d3a4b;
    .el-input__inner{
      border: 1px solid #C0C4CC;
      border-radius: 5px;
    &:focus{
         border: 1px solid #e15536
      }
    }

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      padding-left: 48px;
      height: 50px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }

    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #fff;
  }
}
.el-form-item__content{
      height: 50px;
      background-color: #fff;
      border-radius: 4px;
    }
    .login-button{
    position: relative;
    width: 100%;
    height: 54px;
    margin-bottom: 14px;
    margin-top: 22px;
    background-color: #e15536;
    border: 0;
    font-size: 18px;
    color: #fff;
    font-weight: 700;
    .load{
      position: absolute;
      display: flex;
      justify-content: center;
      align-items: center;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      &::before{
        margin-top: 20px;
      }
      .el-loading-spinner{
        margin-top: -9px;
        left: -33px;
      }
      .el-loading-mask{
        background-color: rgba(255,255,255,.6);
      }
    }
    }
      .el-form-item.is-error .el-input__inner,
    .el-form-item.is-error .el-textarea__inner {
  border-color: red !important;
     }

</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.el-button--primary{
  background-color: #e15536 !important;
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    background-color: #fff;
    position: absolute;
    top: 45%;
    left: 45%;
    width: 450px;
    height: 490px;
    padding: 36px 40px;
    margin: -180px 0 0 -160px;
    border-radius: 10px;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    width: 21px;
    height: 21px;
    position: absolute;
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;

  }

  .title-container {
    margin-bottom: 28px;
    display: flex;
    justify-content: center;
    width: 370px;
    height: 58px;
      img{
        height: 54px

    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

video {
  position: fixed;
  top: 50%;
  left: 50%;
  min-width: 100%;
  min-height: 100%;
  width: auto;
  height: auto;
  transform: translateX(-50%) translateY(-50%);
  z-index: -1;
}
.el-form-item__content{
 .code-image{
    display: inline-block;
    vertical-align: top;
    width: 71px;
    height: 32px;
    cursor: pointer;
    margin-left: 10px;
    position: absolute;
    right: 10px;
    top: 9px;
    }
}
</style>
